package com.yuncheng.oaApi.vo;

import com.yuncheng.utils.I18nUtil;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;


public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    @ApiModelProperty("成功标志")
    private boolean success = true;
    @ApiModelProperty("返回处理消息")
    private String message = "";
    @ApiModelProperty("返回代码")
    private Integer code = 0;
    @ApiModelProperty("返回数据对象")
    private T result;
    @ApiModelProperty("时间戳")
    private long timestamp = System.currentTimeMillis();

    public Result() {
    }

    public Result(boolean success) {
        if (success) {
            this.success = true;
            this.code = Type.SUCCESS.value;
        } else {
            this.success = false;
            this.code = Type.ERROR.value;
        }

    }

    public Result<T> success() {
        this.code = Type.SUCCESS.value;
        this.success = true;
        return this;
    }

    public Result<T> success(String message) {
        this.message = message;
        this.code = Type.SUCCESS.value;
        this.success = true;
        return this;
    }

    public Result<T> error500(String message) {
        this.message = message;
        this.code = Type.ERROR.value;
        this.success = false;
        return this;
    }

    public static Result<Object> ok() {
        Result<Object> Result = new Result(true);
        Result.setMessage(I18nUtil.message("operate.success", new Object[0]));
        return Result;
    }

    public static Result<Object> ok(String msg) {
        Result<Object> Result = new Result(true);
        Result.setMessage(msg);
        return Result;
    }

    public static Result<Object> ok(Object data) {
        Result<Object> Result = new Result(true);
        Result.setResult(data);
        Result.setMessage(I18nUtil.message("operate.success", new Object[0]));
        return Result;
    }

    public static Result<Object> error(String msg) {
        Result<Object> Result = new Result(false);
        Result.setMessage(msg);
        return Result;
    }

    public static Result<Object> error(int code, String msg) {
        Result<Object> Result = new Result(false);
        Result.setCode(code);
        Result.setMessage(msg);
        return Result;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
        if (success) {
            this.code = Type.SUCCESS.value;
        } else {
            this.code = Type.ERROR.value;
        }

    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getCode() {
        return this.code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public T getResult() {
        return this.result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public long getTimestamp() {
        return this.timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public static enum Type {
        SUCCESS(200),
        ERROR(500);

        private final Integer value;

        private Type(Integer value) {
            this.value = value;
        }

        public Integer value() {
            return this.value;
        }
    }
}
